package strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付上下文类
 * 用于管理和切换不同的支付策略
 */
@Component
public class PaymentContext {
    private PaymentStrategy paymentStrategy;
    private final PaymentStrategyFactory strategyFactory;

    @Autowired
    public PaymentContext(PaymentStrategyFactory strategyFactory) {
        this.strategyFactory = strategyFactory;
        // 默认使用现金支付
        this.paymentStrategy = strategyFactory.getStrategy("cashPayment");
    }

    /**
     * 根据策略名称切换支付策略
     * @param strategyName 策略Bean名称
     */
    public void setStrategy(String strategyName) {
        this.paymentStrategy = strategyFactory.getStrategy(strategyName);
    }

    /**
     * 执行支付操作
     * @param amount 支付金额
     */
    public void executePayment(double amount) {
        paymentStrategy.pay(amount);
    }
}